package com.gmq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gmq.common.BaseContext;
import com.gmq.common.R;
import com.gmq.entity.ShoppingCart;
import com.gmq.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private BaseContext context;

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        long id = context.getThreadLocal();
        queryWrapper.like(ShoppingCart::getUserId, id);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加购物车{}", shoppingCart);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        long id = context.getThreadLocal();
        //判断是套餐还是菜品
        if (dishId != null) {
            //动态添加条件
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        queryWrapper.like(ShoppingCart::getUserId, id);
        //查询当前菜品或套餐是否在购物车中
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(queryWrapper);
        //如果存在增加数量
        if (shoppingCartServiceOne != null) {
            Integer number = shoppingCartServiceOne.getNumber() + 1;
            shoppingCartServiceOne.setNumber(number);
            shoppingCartService.updateById(shoppingCartServiceOne);
        } else {
            //不存在新增菜品
            shoppingCart.setUserId(id);
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartServiceOne = shoppingCart;
        }
        return R.success(shoppingCartServiceOne);
    }

    @DeleteMapping("/clean")
    public R<String> clean() {
        log.info("清空购物车");
        long id = context.getThreadLocal();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ShoppingCart::getUserId, id);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("删除商品{}", shoppingCart);
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if (dishId != null) {
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        //数量小于1直接删除
        Integer number = cartServiceOne.getNumber();
        if (number == 1) {
            shoppingCartService.removeById(cartServiceOne.getId());
        } else {
            //大于1，数量减1
            number -= 1;
            cartServiceOne.setNumber(number);
            shoppingCartService.updateById(cartServiceOne);
        }
        return R.success("删除成功");
    }
}
